package com.yanqu.road.server.cmd.activity.tonbing.cross;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.TonBingProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;
import io.netty.channel.ChannelHandlerContext;


@Cmd(code = GameProtocol.S_CROSS_TON_BING_BARRACKS_OP, desc = "")
public class TonBingBarracksOpCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        TonBingProto.CrossTonBingBarracksOpMsg reqMsg = TonBingProto.CrossTonBingBarracksOpMsg.parseFrom(packet.getBytes());
        long userId = packet.getPlayerId();
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
        if (player != null) {

            if (reqMsg.getType() == 1) {
                if (!StringUtils.isNullOrEmpty(reqMsg.getCost())) {
                    player.getModule(PreReduceModule.class).restorePreReduce(reqMsg.getCost());
                    player.getModule(CurrencyModule.class).removeCurrency(PropertyHelper.parseStringToProperty(reqMsg.getCost()),
                            eLogMoneyType.TonBing, eLogMoneyType.TonBingBarracksCallSoldier);
                }
            } else if (reqMsg.getType() == 2) {
                if (!StringUtils.isNullOrEmpty(reqMsg.getCost())) {
                    player.getModule(PreReduceModule.class).restorePreReduce(reqMsg.getCost());
                }
            } else if (reqMsg.getType() == 3) {
                // 无事发生
            } else if (reqMsg.getType() == 4) {
                if (!StringUtils.isNullOrEmpty(reqMsg.getCost())) {
                    player.getModule(CurrencyModule.class).addCurrency(reqMsg.getCost(), eLogMoneyType.TonBing, eLogMoneyType.TonBingBarracksCancel);
                }
            }
            player.sendPacket(ClientProtocol.U_TON_BING_RECRUIT_SOLDIER, reqMsg.getResMsg().toBuilder());
        }
    }

}
